<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-07 16:19:40
 * @LastEditors: your name
 * @LastEditTime: 2021-06-23 14:22:55
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? $t("sys.new_sub_user_group") : $t("sys.edit_user_group") }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <el-form class="pl30 pr30" :model="formData" ref="form" label-width="120px">
      <el-form-item
        v-if="defaultConfig.root !== formData.parentId"
        :label="$t('sys.parent_user_group')"
        prop="parentId"
        :rules="[
          { required: true, message: $t('sys.select_parent_user_group'), trigger: 'change' },
        ]"
      >
        <tree-select
          style="width: 400px"
          v-model="formData.parentId"
          :treeData="isNew ? treeData : orgTree"
        />
      </el-form-item>

      <el-form-item
        :label="$t('sys.user_group_name')"
        prop="name"
        :rules="[
          { required: true, message: $t('sys.user_group_name_not_null'), trigger: 'change' },
        ]"
      >
        <el-input
          v-model="formData.name"
          style="width: 400px"
          :placeholder="$t('sys.enter_user_group_name')"
          :maxlength="64"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">{{$t('sys.cancel')}}</el-button>
      <el-button type="primary" :loading="loading" @click="submit">{{$t('sys.confirm')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import lodash from 'lodash'
import { postHmusergroup, putHmusergroup } from '@/apis/paas/HmUserGroupController.js'

function disabledTreeNode (tree, value) {
  return lodash.cloneDeep(tree).map((node) => {
    node.disabled = node._parentIds.includes(value) || node.id === value
    node.children = disabledTreeNode(node.children, value)
    console.log(node)
    return node
  })
}

export default {
  props: {
    treeData: {
      type: Array,
      default: () => []
    },
    defaultConfig: {
      type: Object,
      default: () => ({})
    }
  },

  data () {
    return {
      loading: false,
      showVisible: false,
      formData: {}
    }
  },

  computed: {
    isNew () {
      return !this.formData.id
    },
    orgTree () {
      const id = this.formData.id
      return disabledTreeNode(this.treeData, id)
    }
  },

  methods: {
    addForm (record = {}) {
      this.showVisible = true
      this.formData = { ...record }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

    editForm (record) {
      this.formData = { ...record }
      this.showVisible = true
    },

    async submit () {
      this.loading = true
      try {
        await this.$refs.form.validate()
        const { id, parentId, name } = this.formData
        let params = {}
        let request = null
        if (this.isNew) {
          params = { name, parentId }
          request = postHmusergroup
        } else {
          params = { id, parentId, name }
          request = putHmusergroup
        }
        for (const key in params) {
          params[key] =
            typeof params[key] === 'string' ? params[key].trim() : params[key]
        }
        request(params).then((res) => {
          this.showVisible = false
          this.$emit('ok')
        })
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
